/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function initLayoutManager() {
    window.addEventListener("orientationchange", onOrientationChange, false);
    window.addEventListener("deviceorientation", onDeviceOrientationChange, false);
    document.ontouchmove = function(e) {
        e.preventDefault();
    };
    $("#widgets").on("touchmove", function(e) {
        e.stopPropagation();
    });
    $("#mainview").on("touchmove", function(e) {
        e.stopPropagation();
    });
    onOrientationChange();
}
var lastOrientation;
var orientation;
var beta = 0;
var gamma = 0;
var deviceHeight;
var deviceWidth;
function onDeviceOrientationChange(e) {
    beta = (e.beta);
    gamma = (e.gamma);
    switch (orientation) {
        case 0:
            $("body").css("background-position", Math.round(gamma*-1) + "px " + Math.round(beta*-1) + "px");
            break;
        case 90:
            $("body").css("background-position", Math.round((beta*-1)) + "px " + Math.round(gamma-90) + "px");
            break;
        case -90:
            $("body").css("background-position", Math.round(beta) + "px " + Math.round(gamma-90) + "px");
            break;
        case 180:
        case -180:
            $("body").css("background-position", Math.round(gamma) + "px " + Math.round(beta) + "px");
            break;
    }
}
function onOrientationChange() {
    if (orientation === undefined) {
        orientation = 0;
    }
    if (lastOrientation !== orientation || lastOrientation === undefined) {

        lastOrientation = orientation;
        // - 20 because of the status bar
        deviceHeight = $(window).height() - 20;
        deviceWidth = $(window).width();
        switch (orientation) {
            case 0: // Portrait
                $("#header").css("float", "none");
                $("#header").css("height", "50px");
                $("#header").css("width", (deviceWidth-11) + "px");
                $("#widgets").css("overflow-x", "auto");
                $("#widgets").css("width", (deviceWidth - 70) + "px");
                $("#widgets").css("overflow-y", "hidden");
                $("#widgets").css("white-space", "nowrap");
                $("#mainview").css("float", "none");
                $("#mainview").css("display", "inline-block");
                $("#mainview").css("width", (deviceWidth) + "px");
                $("#mainview").css("height", (deviceHeight - 125) + "px");
                $("#ads").css("width", (deviceWidth) + "px");
                $("#ads").css("height", "70px");
                $("#ads").css("display", "inline-block");
                $("#ads").css("float", "none");
                $("#dummyAlert").css("width","100%");
					 $("#Alert").css("width","100%");
                break;
            case 90: // Landscape Left
                $("#header").css("float", "left");
                $("#header").css("height", deviceHeight + "px");
                $("#header").css("width", "50px");
                $("#header").css("overflow", "hidden");
                $("#widgets").css("white-space", "normal");
                $("#widgets").css("height", (deviceHeight - 20) + "px");
                $("#widgets").css("width", "50px");
                $("#widgets").css("overflow", "auto");
                //$("#widgets").css("overflow-x", "scroll");
                //$("#widgets").css("overflow-y", "scroll");
                $("#mainview").css("float", "none");
                $("#mainview").css("display", "inline-block");
                //$("#mainview").css("background-color", "#00aa00");
                $("#mainview").css("width", (deviceWidth - 75) + "px");
                $("#mainview").css("height", (deviceHeight - 50) + "px");
                $("#ads").css("width", (deviceWidth - 70) + "px");
                $("#ads").css("height", "70px");
                $("#ads").css("display", "inline-block");
                //$("#ads").css("background-color", "#aa0000");
                $("#ads").css("float", "none");
					 $("#dummyAlert").css("float","right");
					 $("#Alert").css("float","right");
				 	 $("#dummyAlert").css("width",(deviceWidth - 75) + "px");
				  	 $("#Alert").css("width",(deviceWidth - 75) + "px");
                
                //widgetConfig("clear","heading,widgetHelp")
                
                break;
            case -90: // Landscape Right
                $("#header").css("float", "right");
                $("#header").css("height", deviceHeight + "px");
                $("#header").css("width", "50px");
                $("#header").css("overflow", "hidden");
                $("#widgets").css("white-space", "normal");
                $("#widgets").css("height", (deviceHeight - 20) + "px");
                $("#widgets").css("width", "50px");
                $("#widgets").css("overflow", "auto");
                $("#mainview").css("float", "none");
                $("#mainview").css("display", "inline-block");
                $("#mainview").css("width", (deviceWidth - 75) + "px");
                $("#mainview").css("height", (deviceHeight - 50) + "px");
                $("#ads").css("width", (deviceWidth - 70) + "px");
                $("#ads").css("height", "70px");
                $("#ads").css("display", "inline-block");
                $("#ads").css("float", "none");
					 $("#dummyAlert").css("float","left");
					 $("#Alert").css("float","left");
					 $("#dummyAlert").css("width",(deviceWidth - 75) + "px");
					 $("#Alert").css("width",(deviceWidth - 75) + "px");
                break;
        }
    }
}

